package co.edu.usbcali.unidades.dataaccess.dao;

import co.edu.usbcali.unidades.dataaccess.sessionFactory.HibernateSessionFactory;
import co.edu.usbcali.unidades.modelo.ZonaExtra;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import org.hibernate.criterion.Example;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * A data access object (DAO) providing persistence and search support for
 * ZonaExtra entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class ZonaExtraDAO implements IZonaExtraDAO {
    private static final Log log = LogFactory.getLog(ZonaExtraDAO.class);

    // property constants
    //public static final String  ZNECODIGO = "zneCodigo";
    public static final String ZNECODIGO = "zneCodigo";

    //public static final Long  ZNEDIMENSION = "zneDimension";
    public static final String ZNEDIMENSION = "zneDimension";

    //public static final Long  ZNEVALOR = "zneValor";
    public static final String ZNEVALOR = "zneValor";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            ZonaExtra Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(ZonaExtra instance) {
        log.debug("saving ZonaExtra instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            ZonaExtra Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(ZonaExtra instance) {
        log.debug("deleting ZonaExtra instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            ZonaExtra Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(ZonaExtra instance) {
        log.debug("updating ZonaExtra instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public ZonaExtra findById(String id) {
        log.debug("finding ZonaExtra instance with id: " + id);

        try {
            ZonaExtra instance = (ZonaExtra) getSession()
                                                 .get(ZonaExtra.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding ZonaExtra failed", re);
            throw re;
        }
    }

    public List<ZonaExtra> findByExample(ZonaExtra instance) {
        log.debug("finding ZonaExtra instance by example");

        try {
            List results = getSession().createCriteria("ZonaExtra")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  ZonaExtra entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< ZonaExtra> found by query
        */
    public List<ZonaExtra> findByProperty(String propertyName, Object value) {
        log.debug("finding ZonaExtra instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from ZonaExtra as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<ZonaExtra> findByZneCodigo(Object zneCodigo) {
        return findByProperty(ZNECODIGO, zneCodigo);
    }

    public List<ZonaExtra> findByZneDimension(Object zneDimension) {
        return findByProperty(ZNEDIMENSION, zneDimension);
    }

    public List<ZonaExtra> findByZneValor(Object zneValor) {
        return findByProperty(ZNEVALOR, zneValor);
    }

    /**
    * Find all ZonaExtra entities.
    *
    * @return List<ZonaExtra> all ZonaExtra instances
    */
    public List<ZonaExtra> findAll() {
        log.debug("finding all ZonaExtra instances");

        try {
            String queryString = "from ZonaExtra";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<ZonaExtra> findByCriteria(String whereCondition) {
        log.debug("finding ZonaExtra " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from ZonaExtra model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<ZonaExtra> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in ZonaExtra failed", re);
            throw re;
        }
    }

    public List<ZonaExtra> findPageZonaExtra(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults, String nombreUni) {
        log.debug("finding ZonaExtra findPageZonaExtra");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from ZonaExtra model where model.inmueble.division.unidadResidencial.unrNombre=:nombre order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");
                
                Query queryObject=getSession().createQuery(queryString);
				queryObject.setParameter("nombre", nombreUni);
				return queryObject.setFirstResult(startRow).setMaxResults(maxResults).list();
                /*return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();*/
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from ZonaExtra model where model.inmueble.division.unidadResidencial.unrNombre=:nombre";
                Query queryObject=getSession().createQuery(queryString);
				queryObject.setParameter("nombre", nombreUni);
				return queryObject.setFirstResult(startRow).setMaxResults(maxResults).list();
               /* return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();*/
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberZonaExtra(String nombreUni) {
        log.debug("finding ZonaExtra count");

        try {
            String queryString = "select count(*) from ZonaExtra model where model.inmueble.division.unidadResidencial.unrNombre=:nombre";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter("nombre", nombreUni);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
